<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Dailybooth Class
 * File: dailybooth_class.php
 * Purpose: Getting dailybooth info
 * Todo: Rewrite when Dailybooth gains an API
 *
 */

class Dailybooth {
	private $feed_url = 'http://dailybooth.com/rss/%s.rss';
	public $username;
	
	/**
	* Used to fetch web files
	*
	* @return string or boolean on fail
	* @access public
	*/	
	public function getItems() {
		// fetch contents
		$contents = KernelBaseSystem::fetchWebFile(sprintf($this->feed_url, $this->username));
		
		// did it work?
		if($contents === false) {
			// failed to obtain URL
			// Dailybooth is down?
			return false;
		} else {
			// did work
			// attempt to parse this
			try {
				$xml = new SimpleXMLElement($contents, LIBXML_NOCDATA);
			} catch (Exception $e) {
				// unable to parse contents
				trigger_error($e, E_USER_WARNING);
				$failed = true;
			}
			
			if($failed) {
				// did fail, something went wrong
				return false;
			} else {
				// did parse, attempt to recreate data
				if(isset($xml->channel->item)) {
					// does have an item, is a real account
					$item_array = array(); $i = 0;

					// loop through items
					foreach($xml->channel->item as $value) {
						// reconstruct array
						// get img src location
						$desc_content = (string)$value->description;
						preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i', $desc_content, $pic_src);

						if(!empty($pic_src[1])) {
							// we do have something, make it small though then pass it
							$new_pic_url = str_replace('large', 'small', (string)$pic_src[1]);
							
							// add this into the array
							if($i < 4) {
								// we don't want more than 4
								$item_array[$i] = array('pic_src' => (string)$new_pic_url,
														'pic_link'=> (string)$value->link);
							}
							
							// increment $i
							$i++;
						}
					}
					
					// return the array
					return $item_array;
				} else {
					// failed to get an item
					// a) dailybooth is having issues
					// b) this account doesn't exist
					return false;
				}
			}
		}
	}
}

?>